package com.acmen.easyapi.parse;

import com.acmen.easyapi.annotation.ReqParam;
import com.acmen.easyapi.model.ReqParamNode;
import com.acmen.easyapi.util.AnnotationUtil;
import com.acmen.easyapi.util.StringUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.*;


/**
 * 接口详细说明表格工具
 *
 * @author acmen
 */
@Component
public class RequestBodyParser extends BaseRequestParser {

    public ReqParamNode createParamNode(Method method) {
              List<List<Annotation>> parameterAnnotations = AnnotationUtil.getParameterAnnotations(method);
        Class[] parameterTypes = method.getParameterTypes();
        Type[] genericParameterTypes = method.getGenericParameterTypes();

        // 如果没有参数，则返回null
        if (genericParameterTypes.length == 0) {
            return null;
        }
        // 创建根节点
        ReqParamNode root = null;

        // 遍历每个参数
        for (int i=0;i<genericParameterTypes.length;i++) {
            // 判断参数是否包含RequestBody注解
            RequestBody requestBody = AnnotationUtil.getAnnotation(parameterAnnotations.get(i), RequestBody.class);
            if(requestBody!=null){
                Type genericType = genericParameterTypes[i];
                Class pType = parameterTypes[i];

                // 创建根节点
                root = new ReqParamNode();
                root.setName(ReqParamNode.NODE_BODY);
                root.setLabel("请求体");
                // 设置是否为数组
                root.setArray(Collection.class.isAssignableFrom(pType)||pType.isArray());
                root.setType(pType.getName());
                // 设置类型别名
                root.setTypeAlias(getDataTypeAlias(genericType,new HashMap<>()));
                // 创建子节点
                root.setSubNodes((List)createParamNodes(genericType, new HashMap<>()));
            }
        }

        // 遍历每个参数
        for (int i=0;i<genericParameterTypes.length;i++) {
            // 判断参数是否包含RequestPart注解
            RequestPart requestPart = AnnotationUtil.getAnnotation(parameterAnnotations.get(i), RequestPart.class);
            if(requestPart!=null){
                // 如果根节点为空，则创建根节点
                if(root == null){
                    root = new ReqParamNode();
                    root.setName(ReqParamNode.NODE_BODY);
                    root.setLabel("请求体");
                    root.setArray(false);
                    root.setType(RequestPart.class.getTypeName());
                    root.setSubNodes(new ArrayList<>());
                }
                // 获取ReqParam注解
                ReqParam reqParam = AnnotationUtil.getAnnotation(parameterAnnotations.get(i), ReqParam.class);
                if(reqParam != null){
                    Type genericType = genericParameterTypes[i];
                    Class pType = parameterTypes[i];

                    // 创建子节点
                    ReqParamNode partNode = new ReqParamNode();
                    partNode.setName(requestPart.value());
                    partNode.setLabel(reqParam.label());
                    partNode.setRequired(reqParam.required());

                    // 设置备注信息
                    if(StringUtil.isNotEmpty(reqParam.note())){
                        partNode.setNotes(Arrays.asList(reqParam.note()));
                    }
                    // 设置是否为数组
                    partNode.setArray(Collection.class.isAssignableFrom(pType)||pType.isArray());
                    partNode.setType(pType.getName());

                    // 设置类型别名
                    partNode.setTypeAlias(getDataTypeAlias(genericType,new HashMap<>()));

                    // 将子节点添加到根节点
                    root.getSubNodes().add(partNode);
                }
            }
        }

        // 返回根节点
        return root;

    }
}
